package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.VaccineInfor;
import com.suyuan.service.CompanyInfoService;
import com.suyuan.service.VaccineInfoService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 疫苗信息控制器(物资管理部分)
 */
@Controller
@RequestMapping(value="/admin/vaccineInfo")
public class VaccineInfoController {

    @Resource(name = "companyInfoService")
    private CompanyInfoService companyInfoService;

    @Resource(name = "vaccineInfoService")
    private VaccineInfoService vaccineInfoService;

    /**
     * 列表
     */
    @RequestMapping(value = "/list")
    public String inquiry(String vaccinename, String vaccinetype,String manufactoryid, Page page,ModelMap model){
        //避免即使不选择疫苗类型controller层还是接收到""而不是null这种情况发生
        if(vaccinetype!=null && "".equals(vaccinetype.trim())){
            vaccinetype=null;
        }
        if(manufactoryid!=null && "".equals(manufactoryid.trim())){
            manufactoryid=null;
        }
        PageInfo info =vaccineInfoService.inquiry(vaccinename,vaccinetype,manufactoryid,page);
        //查询所有的公司，用于显示列表显示公司名称而不是gid
        List<CompanyInfo> companyInfos=companyInfoService.findAll();
        //查询所有疫苗公司，用于查询条件的下拉选择
        List<CompanyInfo> companyInfosym=companyInfoService.findByCompTypeId(101);
        model.addAttribute("companyInfosym",companyInfosym);
        model.addAttribute("page",info);
        model.addAttribute("companyInfos",companyInfos);
        return "vaccineInfo_list";
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping( value = "add")
    public String add(ModelMap model){
        //查询疫苗种类，如果将要添加的种类已经存在，则不能添加(种类的判断是通过疫苗名称、疫苗类型和生产企业是否相同来判断)
        List<VaccineInfor> vaccineInfors=vaccineInfoService.findAll();
        StringBuilder sb=new StringBuilder();
        for(VaccineInfor vac:vaccineInfors){
            String vaccinename=vac.getVaccinename();
            String vaccinetype=vac.getVaccinetype();
            String manufactoryid=vac.getManufactoryid();
            sb.append(vaccinename+"-"+vaccinetype+"-"+manufactoryid+";");
        }
        //疫苗种类信息
        String ymzlAll=sb.toString();
        System.out.println(ymzlAll);
        model.addAttribute("ymzlAll",ymzlAll);
        List<CompanyInfo> companyInfos=companyInfoService.findByCompTypeId(101);
        model.addAttribute("companyInfos",companyInfos);
        return "vaccineInfo_add";
    }

    /**
     * 保存
     * @return
     */
    @RequestMapping( value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Object save(VaccineInfor vaccineInfor){
        int code = 0;
        try {
            code = vaccineInfoService.save(vaccineInfor);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping( value = "delete")
    @ResponseBody
    public Object delete(Integer id){
        int code = vaccineInfoService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }
        return JsonMessage.error("删除失败");
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            vaccineInfoService.delete(Integer.parseInt(id));
        }
        return null;
    }

    /**
     * 跳到编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer vaccineid,ModelMap modelMap){
        //查询疫苗种类，如果将要添加的种类已经存在，则不能修改(种类的判断是通过疫苗名称、疫苗类型和生产企业是否相同来判断)
        List<VaccineInfor> vaccineInfors=vaccineInfoService.findAll();
        StringBuilder sb=new StringBuilder();
        for(VaccineInfor vac:vaccineInfors){
            String vaccinename=vac.getVaccinename();
            String vaccinetype=vac.getVaccinetype();
            String manufactoryid=vac.getManufactoryid();
            sb.append(vaccinename+"-"+vaccinetype+"-"+manufactoryid+";");
        }
        //疫苗种类信息
        String ymzlAll=sb.toString();
        modelMap.addAttribute("ymzlAll",ymzlAll);

        VaccineInfor vaccineInfo=vaccineInfoService.findByVaccineId(vaccineid);
        List<CompanyInfo> companyInfos=companyInfoService.findByCompTypeId(101);
        modelMap.addAttribute("vaccineInfo",vaccineInfo);
        modelMap.addAttribute("companyInfos",companyInfos);
        return "vaccineInfo_edit";
    }

    /**
     * 保存
     * @return
     */
    @RequestMapping( value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(VaccineInfor vaccineInfo){
        int code = 0;
        try {
            code = vaccineInfoService.update(vaccineInfo);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }
}
